package edu.ucsc.gameAI.conditions;

import java.util.Vector;

import pacman.game.Constants.DM;
import pacman.game.Constants.GHOST;
import pacman.game.Game;
import edu.ucsc.gameAI.ICondition;
import edu.ucsc.gameAI.PacmanConstants;

public class PowerPillsRemain implements ICondition{

	@Override
	public boolean test(Game game) {
		
		int pacManCurrentNode = game.getPacmanCurrentNodeIndex();
		
		Vector<GHOST> edibleGhosts = new Vector<GHOST>();
		Vector<GHOST> nonEdibleGhosts = new Vector<GHOST>();
		for (GHOST ghost: GHOST.values()){
			//System.out.println("ghost " + ghost + " is currently at node " + game.getGhostCurrentNodeIndex(ghost));
			if(game.isGhostEdible(ghost)){ //If any Ghosts Are Edible
				edibleGhosts.add(ghost); // store which ghosts are edible.	
				//System.out.println("Adding " + ghost + " to vector of edible ghosts");
			}
			else{
				nonEdibleGhosts.add(ghost);
			}
		}
		
		int closestGhostDistance = Integer.MAX_VALUE;
		int closestGhostIndex = -1;
	
		for (GHOST edibleGhost: edibleGhosts){
			int currentGhostIndex = game.getGhostCurrentNodeIndex(edibleGhost);
			if(closestGhostDistance > game.getShortestPathDistance(pacManCurrentNode, currentGhostIndex))
			{
				closestGhostDistance = game.getShortestPathDistance(pacManCurrentNode, currentGhostIndex);
				closestGhostIndex = game.getGhostCurrentNodeIndex(edibleGhost);
			}
		}
		
		switch(game.getActivePowerPillsIndices().length)
		{
		case 3: 
			return game.getDistance(game.getPacmanCurrentNodeIndex(), closestGhostIndex, DM.PATH) < PacmanConstants.edibleGhostDistance - 5;
		case 2: 
			return game.getDistance(game.getPacmanCurrentNodeIndex(), closestGhostIndex, DM.PATH) < PacmanConstants.edibleGhostDistance - 10;
		case 1: 
			return game.getDistance(game.getPacmanCurrentNodeIndex(), closestGhostIndex, DM.PATH) < PacmanConstants.edibleGhostDistance - 15;
		default:
			return false;
		}
	}

}
